﻿using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WPF_Bendler_Demo.Views
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : System.Windows.Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            double offset = myScrollViewer.VerticalOffset;

            // 获取每个分割线的 Y 坐标相对于 ScrollViewer 的位置
            double divider1Offset = targetElement1.TransformToVisual(targetElement1).Transform(new Point(0, 0)).Y;
            double divider2Offset = targetElement2.TransformToVisual(targetElement1).Transform(new Point(0, 0)).Y;
            double divider3Offset = targetElement3.TransformToVisual(targetElement1).Transform(new Point(0, 0)).Y;
            double divider4Offset = targetElement4.TransformToVisual(targetElement1).Transform(new Point(0, 0)).Y;
            double divider5Offset = targetElement5.TransformToVisual(targetElement1).Transform(new Point(0, 0)).Y;

            if (offset < divider2Offset)
            {
                SelectButton(button1);
            }
            else if (offset < divider3Offset)
            {
                SelectButton(button2);
            }
            else if (offset < divider4Offset)
            {
                SelectButton(button3);
            }
            else if (offset < divider5Offset)
            {
                SelectButton(button4);
            }
            else
            {
                SelectButton(button5);
            }
        }

        private void SelectButton(RadioButton selectedButton)
        {
            button1.IsChecked = false;
            button2.IsChecked = false;
            button3.IsChecked = false;
            button4.IsChecked = false;
            button5.IsChecked = false;
            selectedButton.IsChecked = true;
        }

        private void RadioButton_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton checkedRadioButton = sender as RadioButton;
            if (checkedRadioButton != null && checkedRadioButton.Tag != null)
            {
                string targetElementName = checkedRadioButton.Tag.ToString();
                var targetElement = (UIElement)myScrollViewer.FindName(targetElementName);
                if (targetElement != null)
                {
                    GeneralTransform transform = targetElement.TransformToVisual(myScrollViewer);
                    Point positionInScrollViewer = transform.Transform(new Point(0, 0));
                    double targetOffset = positionInScrollViewer.Y + myScrollViewer.VerticalOffset;
                    myScrollViewer.ScrollToVerticalOffset(targetOffset);
                }
            }
        }


        private void DataTemplate_Checked(object sender, RoutedEventArgs e)
        {
            RadioButton radioButton = sender as RadioButton;
            if (radioButton != null && radioButton.Tag != null)
            {
                string selectedTemplateKey = radioButton.Tag.ToString();
                ContentDisplay.ContentTemplate = (DataTemplate)FindResource(selectedTemplateKey);
            }
        }
    }
}
